package com.otod.pm.doc.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.pm.doc.mapper.PmDocTpMasterMapper;
import com.otod.pm.doc.vo.PmDocTpItemVo;
import com.otod.pm.doc.vo.PmDocTpMasterVo;

@Service
public class DocTpMasterService extends BaseService{
	protected static final Logger logger = LoggerFactory.getLogger(DocTpMasterService.class);
	
	@Autowired  
	private PmDocTpMasterMapper docTpMasterMapper;
	
	@Autowired
	private DocTpService docTpService;
	
	
	/**
	 * 通过uuid查询数据
	 * @param id
	 * @return
	 */
	public PmDocTpMasterVo getById(String id){
		return docTpMasterMapper.getById(id);
	}
	
	
	/**
	 * 查询菜单列表
	 */
	public List<PmDocTpMasterVo> queryForList(Map<String, Object> params){
		return docTpMasterMapper.queryForList(params);
	}
	
	
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@Transactional
	public void deleteById(String tpId) throws AppException{
		docTpService.deleteByTpId(tpId);
		docTpMasterMapper.deleteById(tpId);
	}
	
	
	
	
	
	/**
	 * 新增
	 */
	@Transactional
	public void insert(PmDocTpMasterVo pmDocTpMasterVo){
		pmDocTpMasterVo.setUuid(super.getUUID());
		docTpMasterMapper.insert(pmDocTpMasterVo);
	}
	
	/**
	 * 修改
	 */
	@Transactional
	public void update(Map map){
		docTpMasterMapper.update(map);
	}
	
	
	
	/**
	
	
	/**
	 * 子节点菜单递归
	 * @param pMenuId
	 * @param childrenList
	 * @param allList
	 */
	private void getChildrenMenu(String pMenuId,List<PmDocTpItemVo> childrenList,List<PmDocTpItemVo> allList){
		for (PmDocTpItemVo docTpItemVo : allList) {
			if(pMenuId.equals(docTpItemVo.getParentId())){
				childrenList.add(docTpItemVo);
				getChildrenMenu(docTpItemVo.getUuid(), childrenList, allList);
			}
		}
	}
	/**
	 * 新增的时候 判断名字是否重复
	 * @param docTpMasterVo
	 * @throws AppException 
	 */
	public void selectRepetitionTpName(PmDocTpMasterVo docTpMasterVo) throws AppException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tpName", docTpMasterVo.getTpName());
		
		map.put("prjType", docTpMasterVo.getPrjType());
		List<PmDocTpMasterVo> docTpItemVos = docTpMasterMapper.queryForList(map);
	
		if(!docTpItemVos.isEmpty()){
			throw new AppException("文档名字重复请重新输入");
		}
		
	}
}
